package com.hermit.qmst.adapter;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hermit.qmst.R;
import com.hermit.qmst.UI.activity.CallingActivity;
import com.hermit.qmst.UI.activity.SelectCallActivity;
import com.hermit.qmst.mode.CallLogInfo;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.Preferences;
import com.hermit.qmst.tools.SipCallHelp;

import java.util.ArrayList;

/**
 * Created by 唐序晓 on 15/2/13.
 */
public class CallLogAdapter extends BaseAdapter {


    private ArrayList<CallLogInfo> mList;
    private Activity mContext;

    public CallLogAdapter(Activity context,ArrayList<CallLogInfo> list){

        this.mContext = context;
        this.mList = list;
    }

    @Override
    public int getCount() {
        return this.mList.size();
    }

    @Override
    public Object getItem(int position) {
        return this.mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

         Holder holder = null;

        if (convertView == null){
             holder = new Holder();
            convertView = LayoutInflater.from(this.mContext).inflate(R.layout.item_list_calllog,null);

            //初始化控件
            holder.tvCallName = (TextView)convertView.findViewById(R.id.tv_call_name);
            holder.tvCallPhone = (TextView)convertView.findViewById(R.id.tv_call_phone);
            holder.tvCallAera = (TextView)convertView.findViewById(R.id.tv_call_area);
            holder.tvCallCount = (TextView)convertView.findViewById(R.id.tv_call_count);
            holder.ivCallType = (ImageView)convertView.findViewById(R.id.iv_call_type);
            holder.rlytItem = (View)convertView.findViewById(R.id.rlyt_item);
            holder.ivCall = (ImageView)convertView.findViewById(R.id.iv_call);
            holder.lytItem = (View)convertView.findViewById(R.id.lyt_item);
            holder.tvCallTime = (TextView)convertView.findViewById(R.id.tv_call_time);

//            holder.tvAddItem = (TextView)convertView.findViewById(R.id.tv_add_item);
//            holder.tvDelItem = (TextView)convertView.findViewById(R.id.tv_del_item);
//            holder.tvInviteItem = (TextView)convertView.findViewById(R.id.tv_invite_item);
//            holder.tvReturnItem = (TextView)convertView.findViewById(R.id.tv_return_item);

            convertView.setTag(holder);
        }else {
            holder = (Holder)convertView.getTag();
        }

        //获取CallLogInfo对象
        final CallLogInfo info = this.mList.get(position);
        if (info.getName() == null || info.getName().length() == 0){
            holder.tvCallName.setText(info.getPhone1());
        }else {
            holder.tvCallName.setText(info.getName());
        }
        holder.tvCallPhone.setText(info.getPhone1());
        //不是联系人，即显示添加联系人按钮
//        if (info.getName() != null && info.getName().length() > 0) {
//            holder.tvAddItem.setVisibility(View.GONE);
//        }else {
//            holder.tvAddItem.setVisibility(View.VISIBLE);
//        }

        holder.tvCallAera.setText(info.getPhoneAera());
        if (info.getCallCount() > 1){
            holder.tvCallCount.setText("("+info.getCallCount()+")");
        }

        //1、来电 2、去电 3、未接
        if (info.getCallLogType() == 1){
            holder.ivCallType.setImageResource(R.drawable.ic_call_in);
        }else if (info.getCallLogType() == 2){
            holder.ivCallType.setImageResource(R.drawable.ic_call_out);
        }else if (info.getCallLogType() == 3){
            holder.ivCallType.setImageResource(R.drawable.ic_call_mis);
        }

        //计算拨打日期
        if (info.getCalltime() != null){
            Long longDate = Long.valueOf(Long.parseLong(info.getCalltime()));

            try{
                CharSequence date = DateUtils.getRelativeTimeSpanString(longDate.longValue(),System.currentTimeMillis(),
                        DateUtils.MINUTE_IN_MILLIS,DateUtils.FORMAT_ABBREV_RELATIVE);
                holder.tvCallTime.setText(date);

            }catch (Resources.NotFoundException e){

            }
        }

//        //显示细节VIEW
//        if (info.isShow()){
//            holder.lytItem.setVisibility(View.VISIBLE);
//        }else {
//            holder.lytItem.setVisibility(View.GONE);
//        }

        //监听器
        //打电话
        holder.rlytItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Common.mCallPhone = info.getPhone1();
                Common.mCallAera = info.getPhoneAera();
                Common.mCallName = info.getName();

                Preferences p = new Preferences(mContext);
                String type = p.getPreferenceStringValue(Preferences.CALL_WAY);
                if (type.equals("0")){//回拨
                    Intent intent = new Intent(mContext, CallingActivity.class);
                    mContext.startActivity(intent);
                    mContext.overridePendingTransition(R.anim.fling_in_right2left,R.anim.fling_out_left2right);

                }else if (type.equals("1")){//直拨
                    SipCallHelp.placeCallWithOption(null,mContext,info.getPhone1());

                }else if (type.equals("2")){//智能拨打
                    if (Common.checkWIFI(mContext)){
                        SipCallHelp.placeCallWithOption(null, mContext, info.getPhone1());
                    }else {
                        Intent intent = new Intent(mContext, CallingActivity.class);
                        mContext.startActivity(intent);
                        mContext.overridePendingTransition(R.anim.fling_in_right2left,R.anim.fling_out_left2right);

                    }

                }else {

                    Intent intent = new Intent(mContext, SelectCallActivity.class);
                    mContext.startActivity(intent);
                    //mContext.overridePendingTransition(R.anim.fling_in_right2left,R.anim.fling_out_left2right);
                }

            }
        });
        //监听显示Item细节
        holder.ivCall.setTag(holder.lytItem);
        holder.ivCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = (View)v.getTag();
                if (view.isShown()){
                    view.setVisibility(View.GONE);
                    info.setShow(false);
                }else {
                    view.setVisibility(View.VISIBLE);
                    info.setShow(true);
                }


            }
        });


        //item监听
        //添加联系人
//        holder.tvAddItem.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
//                intent.putExtra("phone",info.getPhone1());
//                mContext.startActivity(intent);
//            }
//        });
//        //邀请好友
//        holder.tvInviteItem.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(mContext, InviteActivity.class);
//                intent.putExtra("phone",info.getPhone1());
//                mContext.startActivity(intent);
//
//            }
//        });
        //删除记录
//        holder.tvDelItem.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                boolean b = mContext.getContentResolver().delete(CallLog.Calls.CONTENT_URI,
//                        CallLog.Calls.NUMBER + "=?",new String[]{info.getPhone2()}) > 0;
//                if (b){
//                    Toast.makeText(mContext,"删除成功",Toast.LENGTH_SHORT).show();
//                }else {
//                    Toast.makeText(mContext,"删除失败",Toast.LENGTH_SHORT).show();
//                }
//
//            }
//        });
//
//        //返回
//        holder.tvReturnItem.setTag(holder.lytItem);
//        holder.tvReturnItem.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                View view = (View)v.getTag();
//                view.setVisibility(View.GONE);
//                info.setShow(false);
//            }
//        });

        return convertView;
    }
    //管理显示VIEW控件
    private class Holder{
        private TextView tvCallName;
        private TextView tvCallPhone;
        private TextView tvCallAera;
        private TextView tvCallCount;
        private ImageView ivCallType;
        private View rlytItem;
        private ImageView ivCall;
        private View lytItem;
        private TextView tvCallTime;

        //细节VIEW
//        private TextView tvAddItem;
//        private TextView tvInviteItem;
//        private TextView tvDelItem;
//        private TextView tvReturnItem;
    }


}
